package com.zff.rental.common;

import lombok.Data;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HCY
 */
@Data
public class PageVo<T> {
    /**
     * 页码 1开始
     */
    private int page;

    /**
     * 每页数量
     */
    private int size;

    /**
     * 总页数
     */
    private int totalPages;

    /**
     * 总元素数量
     */
    private long totalElements;

    /**
     * 当前页元素数量
     */
    private int numberOfElements;

    /**
     * 结果数据
     */
    private List<T> content;

    private PageVo(int page, int size, long totalElements, List<T> content) {
        this.page = page;
        this.size = size;
        if (totalElements <= 0) {
            this.totalPages = 0;
            this.totalElements = 0;
        } else {
            this.totalElements = totalElements;
            if (totalElements % size == 0) {
                this.totalPages = (int) (totalElements / size);
            } else {
                this.totalPages = (int) (totalElements / size + 1);
            }
        }
        if (null == content) {
            content = new ArrayList<>();
        }
        this.content = content;
        this.numberOfElements = content.size();
    }

    public static <T> PageVo<T> of(int number, int size, long totalElements, List<T> content) {
        return new PageVo<>(number, size, totalElements, content);
    }

    public static <T> PageVo<T> of(Page<?> page, List<T> content) {
        return new PageVo<>(page.getNumber(), page.getSize(), page.getTotalElements(), content);
    }
}
